package com.syqc.choose.car

import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.comlib.utils.CacheMMKV
import com.syqc.entity.ChooseTeamCar
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.BehaviorSubject

/**
 *@Author Jasper
 *@Time   2020/7/23
 *@Desc
 */
class ChooseCarViewModel : AutoDisposeViewModel() {
    private var pids = mutableListOf("0")
    private var names = mutableListOf<String>()
    var isVideo = false
    var rootName = ""

    private val carTeamList by lazy { mutableListOf<ChooseTeamCar>() }
    private val listObservable by lazy {
        BehaviorSubject.create<MutableList<ChooseTeamCar>>()
    }

    fun listObserver(): Observable<MutableList<ChooseTeamCar>> = listObservable.hide()
    val titleName: BehaviorSubject<String> by lazy { BehaviorSubject.create<String>() }


    fun getTeamCars() {
        toastLoading(true)
        ServiceHelper.getRepository().getTeamCars(pids[pids.lastIndex], isVideo).autoDispose(this)
            .subscribe({
                carTeamList.clear()
                for (team in it.teamList) {
                    if (isVideo) {
                        if (team.carNum != 0) carTeamList.add(
                            ChooseTeamCar(
                                pid = team.pid,
                                teamId = team.teamId,
                                carNum = team.carNum,
                                teamName = team.teamName
                            )
                        )
                    } else {
                        carTeamList.add(
                            ChooseTeamCar(
                                pid = team.pid,
                                teamId = team.teamId,
                                carNum = team.carNum,
                                teamName = team.teamName
                            )
                        )
                    }
                }
                if (!it.carList.isNullOrEmpty()) {
                    for (car in it.carList) {
                        carTeamList.add(
                            ChooseTeamCar(
                                carId = car.carId,
                                carName = car.carName,
                                carPlate = car.carPlate,
                                iconLink = car.iconLink,
                                state = car.state,
                                accState = car.acc,
                                videos = car.videos,
                                teamId = car.teamId,
                                isVideo = !car.videos.isNullOrEmpty()
                            )
                        )
                    }
                }
                toastLoading(false)

                listObservable.onNext(carTeamList)
            }, error)
    }

    //上个车队
    fun getPreviousTeam() {
        if (pids.size <= 1) {
            toast(R.string.desc_root_team)
        } else {
            pids.removeAt(pids.lastIndex)
            names.removeAt(names.lastIndex)
            getTeamCars()
        }
        titleName.onNext(names[names.lastIndex])
    }

    //home
    fun getHomeTeam() {
        if (pids.size <= 1) {
            toast(R.string.desc_root_team_)
        } else {
            pids.clear()
            pids.add("0")
            names.add(rootName)
            getTeamCars()
        }
        titleName.onNext(names[names.lastIndex])
    }

    //下个车队
    fun getNextTeam(pid: String, teamName: String) {
        pids.add(pid)
        names.add(teamName)
        getTeamCars()
        titleName.onNext(names[names.lastIndex])
    }

    /**
     * 保存最后选中的数据
     *
     */
    fun saveData() {
        var ids = ""
        pids.forEach {
            ids = "$ids,$it"
        }
        var nameS = ""
        names.forEach {
            nameS = "$nameS,$it"
        }
        CacheMMKV.encode(if (isVideo) "ChooseVideoCarIds" else "ChooseCarIds", ids.substring(1))
        CacheMMKV.encode(
            if (isVideo) "ChooseVideoCarNames" else "ChooseCarNames",
            nameS.substring(1)
        )
    }

    fun initTeam() {
        val ids = CacheMMKV.decodeString(if (isVideo) "ChooseVideoCarIds" else "ChooseCarIds")
        val nameS = CacheMMKV.decodeString(if (isVideo) "ChooseVideoCarNames" else "ChooseCarNames")
        if (ids.isEmpty()) {
            names.add(rootName)
        } else {
            pids.clear()
            pids.addAll(ids.split(","))
            names.clear()
            names.addAll(nameS.split(","))
        }

        titleName.onNext(names[names.lastIndex])
    }


}
